
import { _decorator, Component, Node, Vec3, CCFloat, Animation, find, Prefab, instantiate, Vec2, CircleCollider2D, RigidBody2D, Contact2DType, Collider2D, IPhysics2DContact, BoxCollider2D, AudioSource, AudioClip, animation } from 'cc';
import { Bullet } from '../Bullet';
import { bulletList, ColliderTag, ObjectLife } from '../Globle';
import { MoveObject } from '../MoveObject';
import { Rocket } from '../Rocket';
import { RocketChild } from '../RocketChild';
import { Thunder } from '../Thunder';
import { Enemy } from './Enemy';
const { ccclass, property } = _decorator;

//钢炮
@ccclass('SteelGun')
export class SteelGun extends Enemy {
    @property(CCFloat) fireDelay:number = 1;
    //子弹速度
    @property(CCFloat) bulletSpeed: number;
    //攻击范围
    @property(CCFloat) fireRadiues:number = 150;
    //攻击距离
    @property(CCFloat) fireDistance:number = 100;
    @property(AudioClip) steel_gun_bomb_audio:AudioClip;
    @property(AudioClip) iron_audio:AudioClip;
    @property(Prefab) bullet:Prefab;
    private player:Node;
    private sprite: Node;
    private animator:animation.AnimationController;
    private rigidbody:RigidBody2D;
    private collider:BoxCollider2D;
    private audioSource:AudioSource;
    private gameScene:Node;
    private playerPosition:Vec3;
    private frime_time:number = 0;
    private isDied:boolean = false;
    private life = ObjectLife.SteelGun;
    start () {
        this.rigidbody = this.getComponent(RigidBody2D);
        this.collider = this.getComponent(BoxCollider2D);
        this.animator = this.getComponent(animation.AnimationController);
        this.audioSource = this.getComponent(AudioSource);
        this.sprite = this.node.children[0];
        this.rigidbody.enabledContactListener = true;
        this.gameScene = find("GameScene");
    }

    public onContact(otherCollider: Collider2D) {
        if(this.isDied) return;
        switch(otherCollider.tag){
            case ColliderTag.playerBullet://玩家的子弹
                let obj:MoveObject = otherCollider.node.getComponent(MoveObject);
                if(!obj) obj = otherCollider.node.getComponent(MoveObject)
                this.life-=obj.ATK;
                this.audioSource.stop();
                if(obj instanceof Bullet){
                    this.audioSource.clip = this.iron_audio;
                }else{
                    this.audioSource.clip = this.steel_gun_bomb_audio;
                }
                if(this.life<=0){
                    this.died();
                }else{
                    this.audioSource.play();
                }
                break;
        }
    }

    update (deltaTime: number) {
        if(this.isDied) return;
        this.frime_time+=deltaTime;
        if(this.frime_time>=this.fireDelay){
            this.fire();
            this.frime_time = 0;
        }
       if(!this.player||!this.player.name){
           if(!(this.player = find("GameScene/Player"))) return;
       }
       this.playerPosition = this.player.position
       this.node.eulerAngles = this.lookAt(this.node,this.player.position);
    }

    //开火
    private fire() {
        let distance = this.node.position.clone().subtract(this.playerPosition).length();
        if(distance>this.fireRadiues) return;
        this.animator.setValue('fire',true);
        let bullet:Node;
        if(bulletList.length){
            bullet = bulletList.find(v=>v.active==false);
            if(!bullet){
                bullet = instantiate(this.bullet);
                bulletList.push(bullet);
                this.gameScene.addChild(bullet);
            }
        }else{
            bullet = instantiate(this.bullet);
            bulletList.push(bullet);
            this.gameScene.addChild(bullet);
        }
        bullet.position = this.node.position;
        bullet.active = true;
        let dx = this.playerPosition.x - this.node.position.x;
        let dy = this.playerPosition.y-this.node.position.y;
        let direction = new Vec2(dx,dy).normalize();
        bullet.getComponent(CircleCollider2D).tag = ColliderTag.enemyBullet;
        let bulletScript = bullet.getComponent(Bullet);
        bulletScript.speed = this.bulletSpeed;
        bulletScript.setDirection(direction,'enemy',this.fireDistance);
    }

    private lookAt(node:Node,targetPos:Vec3) {
        let dx = targetPos.x - node.position.x;
        let dy = node.position.y-targetPos.y;
        let rotationZ = Math.atan2(dx,dy)*180/Math.PI;
        let originRotationZ = node.eulerAngles.z;
        let addRotationZ = rotationZ - originRotationZ;
        if(addRotationZ>180) addRotationZ -=360;
        return new Vec3(0,0,node.eulerAngles.z+addRotationZ);
    }

    //死亡
    private died() {
        this.isDied = true;
        this.collider.tag = ColliderTag.player;
        this.animator.setValue('died',true);
        this.audioSource.clip = this.steel_gun_bomb_audio;
        this.audioSource.play();
        // this.scheduleOnce(()=>this.onDied(),0.01);
    }

    public onDied() {
        this.sprite.active = false;
        this.scheduleOnce(()=>{this.node.destroy();},0.5)
    }
}
